﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RichTextBoxCtrl;
using OpenSoftStore.BusinessLogic.Member;
using OpenSoftStore.Client.Common;
using OpenSoftStore.Client.UIL.ManageCommon;
using OpenSoftStore.Orm.Model;

namespace OpenSoftStore.Client.UIL.ManageMember.Info
{
    public partial class BaseInfo : UserControl
    {
        private static BaseInfo instance;

        public MemberLogic MemberBll;
        public T_Member Member;
        public static BaseInfo Instance
        {
            get { return instance ?? (instance = new BaseInfo()); }
        }

        public BaseInfo()
        {
            InitializeComponent();
            SPACommon.SetUiStyle(this);
            MemberBll=new MemberLogic();
            Member=new T_Member();
        }

        public void GetMember(T_Member member)
        {
            var vMember = MemberBll.GetMemberViewObject(" where T_Member.Id ='" + member.Id + "'");
            SPACommon.SetObjectToControlValue(this, vMember);
            SPACommon.CopyPropertiesFrom(Member, vMember);
        }

        private void tlSave_Click(object sender, EventArgs e)
        {
            SPACommon.SetControlToObjectValue(this, Member);
            if (string.IsNullOrEmpty(Member.CardId))
            {
                FormSysMessage.ShowSuccessMsg("会员卡号不能空！");
                return;
            }
            if (string.IsNullOrEmpty(Member.TreeId))
            {
                FormSysMessage.ShowSuccessMsg("类型不能空！");
                return;
            }
            if (string.IsNullOrEmpty(Member.Name))
            {
                FormSysMessage.ShowSuccessMsg("姓名不能空！");
                return;
            }
            try
            {
                MemberBll.Save(Member);
            }
            catch (Exception exception)
            {
                MessageBox.Show("私密档案不能包含图片");
                return;
            }
            MessageBox.Show("保存成功！");
        }

        private void btnGetTree_Click(object sender, EventArgs e)
        {
            var formTreeSelecter = new FormTreeSelecter();
            formTreeSelecter.GetTreeByType("MemberTree");
            formTreeSelecter.ShowDialog();
            if (formTreeSelecter.DialogResult != DialogResult.OK) return;
            Member.TreeId = formTreeSelecter.TypeId;
            txtTreeName.Text = formTreeSelecter.TypeText;
        }

        private void tlClearPass_Click(object sender, EventArgs e)
        {
            DialogResult message = MessageBox.Show("确定清空客户卡密码？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (message != DialogResult.OK) return;
            Boolean result=MemberBll.ClearPass(Member);
            if (result == false)
            {
                FormSysMessage.ShowMessage("操作失败！");
                return;
            }
            Member.Pass = "";
        }

        private void tlMemberPassEdit_Click(object sender, EventArgs e)
        {
            var memberPassEditWindow = new MemberPassEditWindow();
            memberPassEditWindow.GetMember(Member);
            memberPassEditWindow.Show();
        }
    }
}
